package com.xiaolun;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.net.URL;
import java.util.concurrent.*;

/**
 * @Author 硕鼠
 * @Date 2020/5/7 21:55
 * @Version 1.0
 * @Description Callable接口
 */
public class Test05 implements Callable<Boolean> {
    private String url; //网络图片地址
    private String name; //保存的文件名

    public Test05(String url,String name){
        this.url = url;
        this.name = name;
    }
    @Override
    public Boolean call() throws Exception {
        WebDownloader webDownloader = new WebDownloader();
        try {
            webDownloader.downloader(url, name);
            System.out.println("下载的文件名："+ name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }


    public static void main(String[] args) throws Exception {
        Test05 t1 = new Test05("http://pic.ruiwen.com/allimg/1708/598c8d3752d8939511.jpg?x-oss-process=style/qr.ruiwen", "1.jpg");
        Test05 t2 = new Test05("http://pic.ruiwen.com/allimg/1708/598c8d37882aa94153.jpg?x-oss-process=style/qr.ruiwen", "2.jpg");
        Test05 t3 = new Test05("http://pic.ruiwen.com/allimg/1708/598c8d37c8e9a56734.jpg?x-oss-process=style/qr.ruiwen", "3.jpg");
        // 创建执行服务：
        ExecutorService ser = Executors.newFixedThreadPool(3);

        //提交执行：
        Future<Boolean> r1 = ser.submit(t1);
        Future<Boolean> r2 = ser.submit(t2);
        Future<Boolean> r3 = ser.submit(t3);

        //获取结果：
        boolean rs1 = r1.get();
        boolean rs2 = r2.get();
        boolean rs3 = r3.get();

        //都是返回：true
        System.out.println(rs1);
        System.out.println(rs2);
        System.out.println(rs3);
        // 关闭服务：
        ser.shutdownNow();
    }
}

